
package com.ami.iusb;

import java.io.IOException;
import java.io.PrintStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

public class LFN {
	public byte sequence = 0;
	public byte[] name0_9 = new byte[10];
	public byte attribute = 0;
	public byte type = 0;
	public byte checksum;
	public byte[] name10_21 = new byte[12];
	public short first_cluster = 0;
	public byte[] name22_25 = new byte[4];

	public int lfn_pack_num = 0;
	public byte[] byte_in_filename = new byte[0];

	public LFN() {
		for (int i = 0; i < this.name0_9.length; i++) {
			this.name0_9[i] = 0;
		}
		for (int i = 0; i < this.name10_21.length; i++) {
			this.name10_21[i] = 0;
		}
		for (int i = 0; i < this.name22_25.length; i++) {
			this.name22_25[i] = 0;
		}
		this.attribute = 15;
		this.type = 0;
		this.first_cluster = 0;
	}

	public static byte[] combine(byte[] paramArrayOfByte1, byte[] paramArrayOfByte2) {
		int i = paramArrayOfByte1.length + paramArrayOfByte2.length - 2;
		int j = 0;
		byte[] arrayOfByte = new byte[i];
		System.arraycopy(paramArrayOfByte1, 0, arrayOfByte, 0, paramArrayOfByte1.length);

		System.arraycopy(paramArrayOfByte2, 0, arrayOfByte, paramArrayOfByte1.length, paramArrayOfByte2.length - 2);
		return arrayOfByte;
	}

	public int getLNFByteNum(String paramString) {
		char[] arrayOfChar = paramString.toCharArray();

		int i = 0;
		for (int j = 0; j < arrayOfChar.length; j++) {
			byte[] arrayOfByte = transferToUnicode(arrayOfChar[j]);
			for (int k = 0; k < arrayOfByte.length; k++) {
				if (((arrayOfByte[k] & MBR.UNSIGNED_BYTE_MASK) != 255)
						&& ((arrayOfByte[k] & MBR.UNSIGNED_BYTE_MASK) != 254))
					i++;
			}
			this.byte_in_filename = combine(this.byte_in_filename, arrayOfByte);
		}

		if (i % MBR.LFN_CHAR_LEN_IN_BYTE == 0) {
			this.lfn_pack_num = (i / MBR.LFN_CHAR_LEN_IN_BYTE);
		} else if ((i + 2) % MBR.LFN_CHAR_LEN_IN_BYTE == 0) {
			this.lfn_pack_num = ((i + 2) / MBR.LFN_CHAR_LEN_IN_BYTE);
		} else {
			this.lfn_pack_num = ((i + 2) / MBR.LFN_CHAR_LEN_IN_BYTE + 1);
		}
		return i;
	}

	public byte[] transferToUnicode(char paramChar) {
		Character localCharacter = new Character(paramChar);
		String str = localCharacter.toString();
		char[] arrayOfChar = str.toCharArray();
		ByteBuffer localByteBuffer1 = Charset.forName("unicode").encode(java.nio.CharBuffer.wrap(arrayOfChar));
		byte[] arrayOfByte1 = new byte[localByteBuffer1.remaining()];
		localByteBuffer1.get(arrayOfByte1);

		int i = MBR.ByteArray2Int(arrayOfByte1);
		ByteBuffer localByteBuffer2 = ByteBuffer.allocate(arrayOfByte1.length);
		localByteBuffer2.order(java.nio.ByteOrder.LITTLE_ENDIAN);
		localByteBuffer2.putInt(i);
		byte[] arrayOfByte2 = localByteBuffer2.array();

		return arrayOfByte2;
	}

	public char[] getLFNpackage(RandomAccessFile paramRandomAccessFile, long paramLong, LFN paramLFN) {
		char[] arrayOfChar1 = new char[5];
		char[] arrayOfChar2 = new char[6];
		char[] arrayOfChar3 = new char[2];
		char[] arrayOfChar4 = new char[0];
		try {
			paramRandomAccessFile.seek(paramLong);
			paramLFN.sequence = ((byte) paramRandomAccessFile.readUnsignedByte());

			for (int i = 0; i < arrayOfChar1.length; i++) {
				arrayOfChar1[i] = ((char) MBR.ChangeToLittleEndian(paramRandomAccessFile.readUnsignedShort(), 2,
						MBR.LITTLE_ENDIAN_TYPE));
			}

			paramLFN.attribute = ((byte) paramRandomAccessFile.readUnsignedByte());
			paramLFN.type = ((byte) paramRandomAccessFile.readUnsignedByte());
			paramLFN.checksum = ((byte) paramRandomAccessFile.readUnsignedByte());

			for (int i = 0; i < arrayOfChar2.length; i++) {
				arrayOfChar2[i] = ((char) MBR.ChangeToLittleEndian(paramRandomAccessFile.readUnsignedShort(), 2,
						MBR.LITTLE_ENDIAN_TYPE));
			}

			this.first_cluster = ((short) paramRandomAccessFile.readUnsignedShort());

			for (int i = 0; i < arrayOfChar3.length; i++) {
				arrayOfChar3[i] = ((char) MBR.ChangeToLittleEndian(paramRandomAccessFile.readUnsignedShort(), 2,
						MBR.LITTLE_ENDIAN_TYPE));
			}

			for (int i = 0; i < MBR.LFN_CHAR_SIZE; i++) {
				if (i < 5) {
					if (arrayOfChar1[i] == 0)
						break;
					arrayOfChar4 = extend(arrayOfChar4, arrayOfChar1[i]);

				} else if (i < 11) {
					if (arrayOfChar2[(i - 5)] == 0)
						break;
					arrayOfChar4 = extend(arrayOfChar4, arrayOfChar2[(i - 5)]);

				} else {

					if (arrayOfChar3[(i - 11)] == 0)
						break;
					arrayOfChar4 = extend(arrayOfChar4, arrayOfChar3[(i - 11)]);
				}

			}

		} catch (IOException localIOException) {
			System.out.println(localIOException);
		}
		return arrayOfChar4;
	}

	public static byte LFN_checksum(RootDirectory paramRootDirectory) {
		byte i = 0;
		byte[] arrayOfByte = new byte[11];

		System.arraycopy(paramRootDirectory.filename, 0, arrayOfByte, 0, paramRootDirectory.filename.length);
		System.arraycopy(paramRootDirectory.ext, 0, arrayOfByte, paramRootDirectory.filename.length,
				paramRootDirectory.ext.length);

		for (int j = 0; j < 11; j++) {
			i = MBR.intToByte(((i & 0x1) << 7 | (i & 0xFE) >> 1) + (arrayOfByte[j] & MBR.UNSIGNED_BYTE_MASK));
		}
		return i;
	}

	public static boolean LFNCheck(RandomAccessFile paramRandomAccessFile, long paramLong, byte paramByte) {
		int i = 0;
		char[] arrayOfChar1 = new char[5];
		int j = 0;
		int k = 0;
		byte b = 0;
		char[] arrayOfChar2 = new char[6];
		int m = 0;
		char[] arrayOfChar3 = new char[2];
		try {
			paramRandomAccessFile.seek(paramLong);
			i = (byte) paramRandomAccessFile.readUnsignedByte();

			for (int n = 0; n < arrayOfChar1.length; n++) {
				arrayOfChar1[n] = ((char) MBR.ChangeToLittleEndian(paramRandomAccessFile.readUnsignedShort(), 2,
						MBR.LITTLE_ENDIAN_TYPE));
			}
			j = (byte) paramRandomAccessFile.readUnsignedByte();
			k = (byte) paramRandomAccessFile.readUnsignedByte();
			b = (byte) paramRandomAccessFile.readUnsignedByte();

			for (int n = 0; n < arrayOfChar2.length; n++) {
				arrayOfChar2[n] = ((char) MBR.ChangeToLittleEndian(paramRandomAccessFile.readUnsignedShort(), 2,
						MBR.LITTLE_ENDIAN_TYPE));
			}
			m = (short) paramRandomAccessFile.readUnsignedShort();

			for (int n = 0; n < arrayOfChar3.length; n++) {
				arrayOfChar3[n] = ((char) MBR.ChangeToLittleEndian(paramRandomAccessFile.readUnsignedShort(), 2,
						MBR.LITTLE_ENDIAN_TYPE));
			}
		} catch (IOException localIOException) {
			System.out.println(localIOException);
		}
		if ((j == 15) && (b == paramByte)) {
			return true;
		}

		return false;
	}

	public static final char[] extend(char[] paramArrayOfChar, char paramChar) {
		int i = paramArrayOfChar.length;
		char[] arrayOfChar = new char[i + 1];
		System.arraycopy(paramArrayOfChar, 0, arrayOfChar, 0, i);
		arrayOfChar[i] = paramChar;

		return arrayOfChar;
	}

	public void debug() {
		System.out.printf("0x%2H ", new Object[] { Integer.valueOf(this.sequence & MBR.UNSIGNED_BYTE_MASK) });
		for (int i = 0; i < 10; i++)
			System.out.printf("0x%2H ", new Object[] { Integer.valueOf(this.name0_9[i] & MBR.UNSIGNED_BYTE_MASK) });
		System.out.printf("0x%2H ", new Object[] { Integer.valueOf(this.attribute & MBR.UNSIGNED_BYTE_MASK) });
		System.out.printf("0x%2H ", new Object[] { Integer.valueOf(this.type & MBR.UNSIGNED_BYTE_MASK) });
		System.out.printf("0x%2H ", new Object[] { Integer.valueOf(this.checksum & MBR.UNSIGNED_BYTE_MASK) });
		for (int i = 0; i < 12; i++) {
			System.out.printf("0x%2H ", new Object[] { Integer.valueOf(this.name10_21[i] & MBR.UNSIGNED_BYTE_MASK) });
			if (i == 1)
				System.out.println();
		}
		System.out.printf("0x%2H ", new Object[] { Short.valueOf(this.first_cluster) });
		System.out.printf("0x%2H ", new Object[] { Short.valueOf(this.first_cluster) });
		for (int i = 0; i < 4; i++)
			System.out.printf("0x%2H ", new Object[] { Integer.valueOf(this.name22_25[i] & MBR.UNSIGNED_BYTE_MASK) });
		System.out.println();
		System.out.println();
	}
}

/*
 * Location: D:\Users\tianlin\Downloads\JViewer.jar!\com\ami\iusb\LFN.class Java
 * compiler version: 5 (49.0) JD-Core Version: 0.7.1
 */